Skip to content

http2: emit error on canceled streams when aborted event is not emitted#62923

Open
mcollina wants to merge 4 commits intonodejs:mainfrom
mcollina:fix-http2-cancel-stream-error
Open

http2: emit error on canceled streams when aborted event is not emitted#62923
mcollina wants to merge 4 commits intonodejs:mainfrom
mcollina:fix-http2-cancel-stream-error

Conversation

@mcollina
Copy link
Copy Markdown
Member

When a client HTTP/2 stream's writable side is already ended (e.g. GET requests), receiving RST code 8 (NGHTTP2_CANCEL) emitted neither the aborted nor the error event, causing the stream to close silently.

The fix only skips error generation for NGHTTP2_CANCEL when the aborted event was actually emitted.

Fixes: #56627

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http2
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added http2 Issues or PRs related to the http2 subsystem. needs-ci PRs that need a full CI run. labels Apr 24, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 24, 2026

Codecov Report

❌ Patch coverage is 71.42857% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.64%. Comparing base (58a8e1d) to head (a3d2cdc).
⚠️ Report is 145 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/http2/core.js 71.42% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62923      +/-   ##
==========================================
- Coverage   89.69%   89.64%   -0.05%     
==========================================
  Files         706      708       +2     
  Lines      218222   220431    +2209     
  Branches    41768    42286     +518     
==========================================
+ Hits       195731   197606    +1875     
- Misses      14411    14668     +257     
- Partials     8080     8157      +77     
Files with missing lines Coverage Δ
lib/internal/http2/core.js 94.96% <71.42%> (-0.29%) ⬇️

... and 92 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina mcollina force-pushed the fix-http2-cancel-stream-error branch 3 times, most recently from f3dd59d to 53f5f63 Compare April 27, 2026 10:44
When a client HTTP/2 stream's writable side is already ended (e.g.
GET requests), receiving RST code 8 (NGHTTP2_CANCEL) emitted neither
the 'aborted' nor the 'error' event, causing the stream to close
silently. This happened because the 'aborted' event is only emitted
when the writable side is still open, but the NGHTTP2_CANCEL code was
unconditionally excluded from error generation assuming the 'aborted'
event would cover it.

Fix by only skipping error generation for NGHTTP2_CANCEL when the
'aborted' event was actually emitted.

Fixes: nodejs#56627
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina mcollina force-pushed the fix-http2-cancel-stream-error branch from 53f5f63 to e2f40e3 Compare April 28, 2026 07:35
Copy link
Copy Markdown
Member

@pimterry pimterry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a significant breaking change, since it'll elevate previously ignored client session shutdown into full 'error' events that can crash things (as in the test changes here) among other issues, and it's also unpredictable: behaviour changing depending on write state means there's a race condition for everybody using this API (as noted in the test assertions here).

I think this approach could work, but it'll create problems in the short term anyway, and it seems like it adds complexity to the already confusing behaviour.

If we're going to break things to fix this, what if we fix this API more thoroughly as a cleaner major bump? Right now it's a mess:

  • Behaviour for handling inbound cancellation depends on current dynamic write state, creating race conditions tied to connection write performance (today silent vs aborted, with this change error vs aborted).
  • We don't fire any error event for the cancelled readable streams, and in fact they can end successfully! Today in all cases, or with this PR if writable is still open, if you're reading incoming data (client or server side) and not paying attention to 'aborted' you get an apparently complete stream with 'end' but actually truncated data (this is quite bad).
  • The code references 'aborted' not erroring for HTTP/1 compat, but that makes no sense - AFAICT HTTP/1 does emit errors in equivalent cases, and the 'aborted' event has been deprecated there since Node 16 in favour of 'close'. I think H1 errors everywhere, except if the readable side has been fully consumed (i.e. just on a server where you're already read the whole request body - in that case it just emits 'close' with no error).

We definitely shouldn't emit 'end', and more consistent behaviour that's easier to predict & matches H1 would be nice. How about:

  • We deprecate 'aborted' in H2, just like H1 (leaving the current behaviour for compat in the meantime?).
  • Emit 'error' in all stream reset cases, except if the read-side is fully consumed (so no error for servers when client aborts a response) exactly like H1. No more risk of unexpected truncation, very clear when a stream is aborted uncleanly. If you didn't read everything, you get an error.

That would remove quite a few existing footguns here, leaving clear behaviour that's easier for everybody involved, and still close the original issue (just watch for error to consistently detect aborts). It's breaking, but not so much more than this PR, and it's more consistent with H1 and easier to understand. Needs some thought to confirm the details but I suspect the actually code change is a very similar size to the diff here. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

http2 Issues or PRs related to the http2 subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

http2: confusion with how aborted ClientHttp2Stream is reported

3 participants